package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.WebPage1User;
import com.example.demo.entity.WebPage2User;
import com.example.demo.entity.WebPage4User;
import com.example.demo.mapper.WebPage4UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/webpage4")
//碰到user
public class Page4Controller {
    @Resource
    WebPage4UserMapper webPage4UserMapper;

    @PostMapping
    public Result<?> save(@RequestBody WebPage4User webPage4User){
        webPage4UserMapper.insert(webPage4User);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody WebPage4User webPage4User){
        webPage4UserMapper.updateById(webPage4User);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "0") Integer search,
                              @RequestParam(defaultValue = "0") Integer searchPageId
    ){
        LambdaQueryWrapper<WebPage4User> wrapper= Wrappers.<WebPage4User>lambdaQuery();
        wrapper.eq(WebPage4User::getBlockId,search);
        wrapper.eq(WebPage4User::getPageId,searchPageId);
        wrapper.orderByAsc(WebPage4User::getId);
        Page<WebPage4User> userPage=webPage4UserMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);
        return Result.success(userPage);
    }
    @GetMapping("/pageId={id}")
    public WebPage4User selectById(@PathVariable Long id){
        return webPage4UserMapper.selectById(id);
    }

    @DeleteMapping("/{id}")
    public Result<?> update (@PathVariable Long id){
        webPage4UserMapper.deleteById(id);
        return Result.success();
    }
    @DeleteMapping("/deletebyblockId/{blockId}")
    public Result<?> update2 (@PathVariable Long blockId){
        LambdaQueryWrapper<WebPage4User>wrapper= Wrappers.<WebPage4User>lambdaQuery();
        wrapper.eq(WebPage4User::getBlockId,blockId);
        webPage4UserMapper.delete(wrapper);
        return Result.success();
    }

}
